﻿using System;
using System.Globalization;
using System.Reflection;
using System.Resources;
using System.IO;

namespace lcqwp
{
    /// <summary>
    /// The <see>ResourceLookUpLibrary</see> is a supporting 
    /// class for looking up strings from resource files. It supplies resource strings
    /// related to an assembly for the given key.
    /// </summary>
    public class ResourceLookUpLibrary
    {
        private ResourceManager _resourceManager;
        private readonly string _resourceFile;
        private readonly string _assemblyName;
        private CultureInfo _cultureInfo;

        /// <summary>
        /// The constructor initializes the ResourceLookUpLibrary instance
        /// for a given assmebly and resource file.
        /// </summary>
        /// <param name="assemblyName">the assembly name formatted for loading at runtime</param>
        /// <param name="resourceFile">the filename of the resource file without extension</param>
        public ResourceLookUpLibrary(string assemblyName, string resourceFile)
            : this(assemblyName, resourceFile, CultureInfo.CurrentCulture)
        {
        }

        /// <summary>
        /// The constructor initializes the ResourceLookUpLibrary instance
        /// for a given assmebly, resource file, and culture
        /// </summary>
        /// <param name="assemblyName">the assembly name formatted for loading at runtime</param>
        /// <param name="resourceFile">the filename of the resource file without extension</param>
        /// <param name="cultureInfo">the culture for which to look up resources</param>
        public ResourceLookUpLibrary(string assemblyName, string resourceFile, CultureInfo cultureInfo)
        {
            _resourceFile = resourceFile;
            _assemblyName = assemblyName;
            _cultureInfo = cultureInfo;
        }

        private ResourceManager ResourceManager
        {
            get
            {
                if (_resourceManager == null)
                {
                    Assembly assembly = Assembly.Load(_assemblyName);
                    _resourceManager = new ResourceManager(_resourceFile, assembly);
                }
                return _resourceManager;
            }
        }

        /// <summary>
        /// Returns the string value corresponding to the specified key
        /// in the current resource file
        /// </summary>
        /// <param name="key">the string representing the key of the sought resource value</param>
        /// <returns>the found resource value or the string [$&lt;key&gt;] if not found</returns>
        public string GetLabel(string key)
        {
            return GetLabel(key, string.Format("[${0}]", key));
        }

        /// <summary>
        /// Returns the string value corresponding to the specified key
        /// in the current resource file
        /// </summary>
        /// <param name="key">the string representing the key of the sought resource value</param>
        /// <param name="defaultText">the string to return if key is not found in the 
        /// resource file</param>
        /// <returns>the found resource value or specified default value if not found</returns>
        public string GetLabel(string key, string defaultText)
        {
            string label = defaultText;
            try
            {
                label = ResourceManager.GetString(key, _cultureInfo);
                if (label == null)
                {
                    // TODO warn
                    label = defaultText;
                }
            }
            catch (MissingManifestResourceException ex)
            {
                // TODO ex hndl
            }
            catch (FileNotFoundException ex)
            {
                // TODO ex hndl
            }
            return label;
        }

        private string GetMessage(string label)
        {
            return string.Format("Label: {0}, resource file: {1}, assembly: {2}. ",
                                 label, _resourceFile, _assemblyName);
        }
    }
}
